<?php
global $_GPC, $_W;
$uniacid = $this->_uniacid;
$bid = $_GPC['bid'];
$keyword = $_GPC['keyword'];
$openid = $_W['openid'];
$uid =  $_W['member']['uid'];
if(empty($uid)){
    $fans = mc_fansinfo($openid,0 ,$uniacid  );
    $uid = $fans['uid'];
}
/*获取指定id的经纪人信息*/
if($_GPC['enews'] == 'LoginBrokerInfo'){
      /* 根据uid */
        if(empty($openid)){
             $this->result(1, "openid为空",[]);
        }
         $ret = pdo_fetch("select * from " . tablename("kbwdl_broker") . " where uniacid='$uniacid' and openid='$openid'");
        if(empty($ret)  ){
            $this->result(1, "经纪人资料", $ret);
        }
        /*如果被禁用*/
        if($ret['disabled']==1){
            $this->result(1, "账号被禁用", $ret);
        }
        $ret['avatar'] = tomedia($ret['avatar']);
        $this->result(0, "经纪人资料", $ret);      
}
 elseif ($_GPC['enews'] == 'getinfo') {
            $data  = pdo_fetch("select * from " . tablename("kbwdl_broker") .
                    " where uniacid='$uniacid' and  id='$bid' ");
    
            $data['thumb'] = tomedia($data['avatar']);
            /* 收藏信息 */
            $condition = ' WHERE `uniacid` = :uniacid AND `uid` = :uid';
            $params = array(':uniacid' => $_W['uniacid'], ':uid' => $data['ecuid']);
            $sql = 'SELECT COUNT(*) FROM ' . tablename('kbwdl_favorite') . $condition;
            $data['fav'] = pdo_fetchcolumn($sql . " AND (acttype='fav' or acttype='newshopfav')", $params);
            $data['view'] = pdo_fetchcolumn($sql . " AND acttype='view' ", $params);
            $data['feed'] = pdo_fetchcolumn($sql . " AND acttype='feed' ", $params);
        
            $this->result(0, "经纪人资料", $data);
        }
        /*用于聊天的经纪人资料*/
 elseif ($_GPC['enews'] == 'tallkBroker') {
        if(!isset($_GPC['bid']) || empty($bid)  ){
            $gzhinfo = pdo_fetch("select * from ".tablename("kbwdl_config"). " where placeid=41 and uniacid='$uniacid' and module='kbwdl_shequzufang'");
       
            if(!empty($gzhinfo['conf_value'])){
                $gzh = iunserializer($gzhinfo['conf_value']);
            }else{
               $this->result(1, "获取资料失败", []);
            }
            $bid = $gzh['tallkbid'];
        }
            $data  = pdo_fetch("select * from " . tablename("kbwdl_broker") .
                    " where uniacid='$uniacid' and id>0 and (id='$bid' or openid='$bid')");
    
            $data['thumb'] = tomedia($data['avatar']);
            /* 收藏信息 */
         
        
            $this->result(0, "经纪人资料", $data);
        }        
 /*浏览某个经纪人*/
 elseif ($_GPC['enews'] == 'visiterBroker') {
            $data  = pdo_fetch("select * from " . tablename("kbwdl_broker") .
                    " where uniacid='$uniacid' and (id='$bid' or openid='$bid')");
    
            $data['thumb'] =  tomedia($data['avatar']);             
            $data['ismyself'] = 0;
            if($data['openid'] == $openid){
                $data['ismyself'] = 1;
            }
            /*活跃度*/
            $data['active'] = floor(($data['onclick']+1) / ($data['zan']+ 3));
            /* 收藏信息 */
            $condition = ' WHERE `uniacid` = :uniacid AND `uid` = :uid';
            $params = array(':uniacid' => $_W['uniacid'], ':uid' => $data['ecuid']);
            $sql = 'SELECT COUNT(*) FROM ' . tablename('kbwdl_favorite') . $condition;
            $data['fav'] = pdo_fetchcolumn($sql . " AND (acttype='fav' or acttype='newshopfav')", $params);
            $data['view'] = pdo_fetchcolumn($sql . " AND acttype='view' ", $params);
            $data['feed'] = pdo_fetchcolumn($sql . " AND acttype='feed' ", $params);
            /*更新经纪人房源信息*/
             $this->count_broker_send_num($data['id'],$data['openid']);
          /*2020-07-16增加浏览历史记录*/
        pdo_update(  'kbwdl_broker', array('onclick +=' => 1), array('id' =>  $bid)  );     
        $houseid = intval($bid);
        $fav = pdo_fetch("select id from " . tablename("kbwdl_favorite") . " where ftype=14 and houseid='$houseid' and uniacid='$uniacid' and openid='$openid'");
        $user= $jsondata = array();         
         if(!empty($openid)){
            $user = pdo_fetch("select ecuid,nickname, avatar from". tablename("kbwdl_broker")." where uniacid='$uniacid' and openid='$openid' order by id desc");
            $jsondata = array('nickname'=> $user['nickname'], 'avatar'=> $user['avatar']);
         }
        if (!empty($fav)) {
            pdo_update(
                    'kbwdl_favorite', array('hits +=' => 1, 'last_time' => TIMESTAMP, 'jsondata'=> json_encode($jsondata) ), array('id' => $fav['id'])
            );
        } else {
            $savedata = array(
                'uid' => $uid,
                'openid' => $openid,
                'uniacid' => $uniacid,
                'addtime' => TIMESTAMP,
                'houseid' => $houseid,
                'ftype' => 14,
                'acttype'=>'view',
                'last_time' => TIMESTAMP,
                'title' => $data['nickname'],
                'smalltext' => "浏览 | 经纪人 ",
                'url'=>   '/kbwdl_shequzufang/pages/broker/home?id='.$bid,
               'jsondata'=> json_encode($jsondata)        
             );
             pdo_insert('kbwdl_favorite', $savedata);
        }
        /*先增加完成浏览量*/    
            $this->result(0, "经纪人资料", $data);
        }
        /**
         * 获取前台发布的门店列表
         */
  elseif($_GPC['enews'] == 'secshop'){
       $data = [];
       $res = pdo_fetchall("select id,shopname from ". tablename('kbwdl_shop') ." where `uniacid`='$uniacid' and disabled=0");
       if(!empty($res)){
           foreach($res as $key => $val){
               $data[$val['id']] = $val['shopname'];
           }
       }
       $this->result(0, "门店下拉选项格式", $data);
  } 
  /**
   * 登录微信接口后
   */
  elseif($_GPC['enews'] == 'loginMember'){
     $broker = pdo_fetch("select * from ".tablename("kbwdl_broker"). " where uniacid='$uniacid' and openid='$openid' order by id desc");
     if(empty($broker)){
          $save = array(
           'avatar'=> $_GPC['avatar'],
           'ecuid'=> $_GPC['ecuid'],
           'nickname'=> $_GPC['nickname'],
           'mobile'=> $_GPC['mobile'],
            'groupid'=>0,
            'uid'=> isset($_GPC['uid']) ? $_GPC['uid']:0,
            'openid'=>$openid,
            'uniacid'=> $uniacid
         );
          
           pdo_insert("kbwdl_broker", $save); 
     }else{
         $save = array();
         if($broker['groupid']==0){
           $save['avatar'] = $_GPC['avatar'] ;           
         }
         if( !empty($_GPC['nickname']) && ($broker['nickname'] !== $_GPC['nickname'])){
             $save['nickname'] = $_GPC['nickname'];
         }
         $save['ecuid']= $_GPC['ecuid'];
         
         pdo_update("kbwdl_broker", $save, array('id'=>$broker['id']));
     }
     $this->result(0, "经纪人资料loginMember", $broker); 
     
  }elseif($_GPC['enews'] == 'bindMobile'){
      /**
       * 绑定手机号和昵称
       */
      $broker = pdo_fetch("select * from ".tablename("kbwdl_broker"). " where uniacid='$uniacid' and openid='$openid' order by id desc");
      if(!empty($broker)){
           $id = $broker['id'];  
           $save['mobile'] = trim($_GPC['mobile']);
           if(empty($broker['avatar'])){
                $save['avatar'] = $fans['avatar'];
                $save['nickname'] = $fans['nickname']; 
           }
           if(empty($save['nickname'])){
               $save['nickname'] = '访客';
           }
           pdo_update("kbwdl_broker", $save, array('id'=>$id));
       } 
       
       $this->result(0, "经纪人资料bindMobile", $save); 
  }
  /*个人绑定信息*/
  elseif ($_GPC['enews'] == 'bindMember'){
       $broker = pdo_fetch("select * from ".tablename("kbwdl_broker"). " where uniacid='$uniacid' and openid='$openid' order by id desc");
       $save = array(
           'ischecked'=> 1,
          /* 'avatar'=> $_GPC['avatar'],          
           'nickname'=> $_GPC['nickname'],
           'mobile'=> $_GPC['mobile']*/
      );
    /*是否设置需要审核 2 不需要审核*/
      if($this->module['config']['system']['brokercheck']==2  ){
          $save['ischecked'] = 1;
      }
        if(isset($_GPC['mobile']) && !empty($_GPC['mobile'])) {
               $save['mobile'] = $_GPC['mobile'];
           }
       if(isset($_GPC['avatar']) && !empty($_GPC['avatar'])) {
            $save['avatar'] = $_GPC['avatar'];
           if(strpos($save['avatar'], 'kbwdl_shequzufang') === FALSE){
               $save['avatar'] = tomedia($save['avatar']);
            }else{
               $save['avatar'] = $_W['siteroot'] . '/addons/kbwdl_shequzufang/style/images/get_avatar.png'; 
            }
           }else{
               $save['avatar'] = $fans['avatar'];
           }
        if(isset($_GPC['nickname']) && !empty($_GPC['nickname'])) {
               $save['nickname'] = $_GPC['nickname'];
           }else{
               $save['nickname'] = $fans['nickname']; 
           }  
           $nickname = $save['nickname'];
         
       /*安全监测*/
      $this->_checkPostContent($save); 
      /**/
       if(!empty($broker)){
           $id = $broker['id'];  
           if($broker['groupid']>0){
                unset($save['avatar']);
                unset($save['nickname']);
           }
           pdo_update("kbwdl_broker", $save, array('id'=>$id));
       }else{
           $save['groupid'] = 0;
           $save['openid'] = $openid;
           $save['uniacid'] = $uniacid;
           $save['ecuid'] = $uid;
           if($openid ){
               pdo_insert("kbwdl_broker", $save);  
           }
            
       }
       /*记录被交换和访客的 信息*/
       if(isset($_GPC['parentbid']) && !empty($_GPC['parentbid'])){
           $parent_bid = intval($_GPC['parentbid']);
           $title = $_GPC['infotitle'];
           $pbroker = pdo_fetch("select * from ".tablename("kbwdl_broker"). " where uniacid='$uniacid' and id='$parent_bid'");
           pdo_insert("kbwdl_share_user", array(
               'uid'=> $uid, 
               'openid'=>$openid,
               'parent_openid'=> $pbroker['openid'],
               'parent_uid'=> $pbroker['ecuid'],
               'name'=> $nickname,
               'mobile'=> $save['mobile'],
               'addtime'=> TIMESTAMP,
               'uniacid'=> $uniacid
           ));
           /*给经纪人发消息*/
           if($pbroker['gzhopenid']){
                $this->_send_notice_account('favorite_notice',$nickname, '通过交换名片发来手机号', $save['mobile'], '来源：'.$title, $pbroker['gzhopenid']);
           }
       }
      $this->result(0, "经纪人资料bindMember", $save); 
  }
  elseif ($_GPC['enews'] == 'getwxphone') {
      
        $encrypt_data =  $_GPC['encrypdata'];
        $iv = $_GPC['ivdata'];
      
        $key  =   base64_decode($_SESSION['session_key']);
 
        $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
        if (is_error($result)) {
            $result = ['phoneNumber'=>''];
        }else{
            $result = json_decode($result, true);
        }
          
    
        $this->result(0, "getwxphone", $result);
        /*获取公司经纪人*/
  }elseif($_GPC['enews'] == 'combroker'){
      $id = intval($_GPC['id']);
      $sql_add = " iscompany =2  and newhouse like '%,$id,%'";
          $ret= pdo_fetchall("select * from ".tablename("kbwdl_broker").
               "  where $sql_add   ");
       if(!empty($ret)){
           foreach($ret as $key=>$val){
               if(!strpos($val['avatar'],"/")){
                   $val['avatar'] = "/addons/kbwdl_shequzufang/style/images/get_avatar.png";
               }
                $ret[$key]['secnum'] = $val['secnum']? $val['secnum']:0;
                 $ret[$key]['ernum'] = $val['ernum']? $val['ernum']:0;
               $ret[$key]['avatar'] =  tomedia($val['avatar']);
               /*  $ret[$key]['showtime'] = date('Y年m月d日', strtotime($val['addtime']));
                 */
           }
       }
  
      $this->result(0, "combroker", $ret);
      
  }elseif($_GPC['enews'] == 'checkBindMobile'){
      if(empty($openid)){
           $this->result(2, "请先登录", array());
      }
       $broker = pdo_fetch("select id,openid,nickname,mobile from ".tablename("kbwdl_broker").
               " where uniacid='$uniacid' and openid='$openid' ");
       
       if(empty($broker) || empty($broker['mobile'])){
          $this->result(1, "请先绑定手机号", $broker);
       }
       $this->result( 0, "已经绑定",  $broker);
       /*绑定银行卡*/
  }elseif($_GPC['enews'] == 'bindIdcard'){
        pdo_update(  'kbwdl_broker',
              ['truename' => trim($_GPC['truename']),
               'idcard' => trim($_GPC['idcard']),
                  'bankname' => trim($_GPC['bankname']),
                  'banknumber' => trim($_GPC['banknumber']),
                  ],
              ['id' =>  $bid, 'uniacid'=> $uniacid] );
       $this->result( 0, "绑定成功",  []);
       /*点赞经纪人*/
  }elseif($_GPC['enews'] == 'addzan'){
      $bid = $_GPC['bid'];
      pdo_update(  'kbwdl_broker', array('zan +=' => 1), array('id' =>  $bid, 'uniacid'=> $uniacid)  );  
      $this->result( 0, "点赞成功",  []);
      /*添加 经纪人的 关联楼盘*/
  }elseif($_GPC['enews'] == 'addnewhouse'){
      $newshouse_id = trim($_GPC['newshouse_id']) ;
      $bid = $_GPC['bid'];
      $broker = pdo_fetch("select id,newhouse from ".tablename("kbwdl_broker").
               " where uniacid='$uniacid' and id='$bid' ");
      if(!empty($newshouse_id) && !empty($broker)){
          $ids = $broker['newhouse'] . $newshouse_id.",";
          
          pdo_update(  'kbwdl_broker', array('newhouse' => $ids), array('id' =>  $bid, 'uniacid'=> $uniacid)  );  
      }
       $this->result( 0, "设置成功",  []);
       /*移除 主营楼盘*/
 }elseif($_GPC['enews'] == 'removenewhouse'){
      $newshouse_id = trim($_GPC['newshouse_id']) ;
      $bid = $_GPC['bid'];
      $broker = pdo_fetch("select id,newhouse from ".tablename("kbwdl_broker").
               " where uniacid='$uniacid' and id='$bid' ");
      if(!empty($newshouse_id) && !empty($broker)){
          
          $ids = trim($broker['newhouse'] ,",");
          $ids = explode(",", $ids);
          $nids =  $dot = "";
          for( $i=0; $i< count($ids); $i++){
              if($ids[$i]!== $newshouse_id){
                  $nids.= $dot .$ids[$i];
                  $dot = ",";
              }
          }
          $nids = $nids .",";
          
          pdo_update(  'kbwdl_broker', array('newhouse' => $nids), array('id' =>  $bid, 'uniacid'=> $uniacid)  );  
      }
       $this->result( 0, "设置成功",  []);
  }
  /*添加 二手房租房等信息到 我的店铺记录*/
  elseif($_GPC['enews'] =='addSechouse'){
      $infoid = trim($_GPC['infoid']) ;
      $bid = $_GPC['bid'];
      $ret = pdo_fetch("select id,infoid from ".tablename("kbwdl_actlog").
               " where uniacid='$uniacid' and infoid='$infoid' and ecuid='$bid' and actname='share_with_sechouse'");
      if(empty($ret)){
          $data = [
              'actname'=>'share_with_sechouse',
              'addtime' => date('Y-m-d H:i:s'),
              'ecuid'=> $bid,
              'uniacid'=> $uniacid,
              'acttype'=> 69,
              'note'=> '添加房源到店铺推广',
              'infoid'=> $infoid
          ];
           pdo_insert("kbwdl_actlog", $data);
      }
       $this->result( 0, "添加推广成功",  []);
       /*删除我加入分享的房源*/
  }elseif($_GPC['enews'] =='delSechouse'){
       $bid = $_GPC['bid'];
       $infoid = trim($_GPC['infoid']) ;
       pdo_query("delete from ".tablename("kbwdl_actlog")."  where uniacid='$uniacid'  and ecuid='$bid' and acttype=69 and infoid='$infoid' ");
       $this->result( 0, "删除成功",  []);
      /*获取我的推广房源*/
  }elseif($_GPC['enews']=='getShareSechouse'){
      $bid = $_GPC['bid'];
      $sql ="select id, infoid from ".tablename("kbwdl_actlog").
               " where uniacid='$uniacid'  and ecuid='$bid' and acttype=69 order by id desc limit 20";
        $shares = pdo_fetchall($sql);
        $ids = [];
        $idstr = "";
        if(!empty($shares)){
            foreach($shares as $key=>$val){
                if($val['infoid']>0){
                    array_push($ids, $val['infoid']);
                }
            }
            if(!empty($ids)){
                $idstr = implode(",", $ids);
            }
        }
        if(empty($idstr)){
             $this->result( 0, "没有推广房源", []);
        }
        $condition =" id in ($idstr)";
        $fileds =  $this->_query_sechouse_field();
        $sql = "SELECT $fileds  FROM " . tablename("kbwdl_house") . " where $condition order by update_time desc limit 0, 20";
        $items = pdo_fetchall($sql);
         if (!empty($items)) {
            foreach ($items as $key => $item) {
               
                $val = $this->_format_sechouse($item);
                
                 $items[$key] = $this->_format_show_item($val, $ischarges, $groupid) ;
            }
         }
     $this->result( 0, "店铺的推广房源", $items);
  }
  
?>
